<template>
  <el-dialog :title="hzsForm.id?'修改合作商':'新增合作商'" :visible="hzsVisible" width="50%" @close='closeFn'>
    <span>
      <el-form  label-width="120px" :model="hzsForm" :rules='hzsRules' ref="hzsForm">
        <el-form-item label="合作商名称:" prop="name">
          <el-input
            placeholder="请输入"
            maxlength="10"
            show-word-limit
            v-model="hzsForm.name"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人:" prop="contact">
          <el-input
            placeholder="请输入"
            maxlength="10"
            show-word-limit
            v-model="hzsForm.contact"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系电话:" prop="mobile">
          <el-input
            placeholder="请输入"
            maxlength="11"
            show-word-limit
            v-model="hzsForm.mobile"
          ></el-input>
        </el-form-item>
        <el-form-item label="分成比例(%):" prop="ratio">
           <el-input-number class="fcbl"  controls-position="right" :min="0" :max="100"  v-model="hzsForm.ratio" placeholder='请输入'  style="width:100%"></el-input-number>
        </el-form-item>
        <el-form-item label="账号:" prop="account" v-if='!hzsForm.id'>
          <el-input
            placeholder="请输入"
            maxlength="18"
            show-word-limit
            v-model="hzsForm.account"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password" v-if='!hzsForm.id'>
          <el-input
            placeholder="请输入"
            maxlength="20"
            show-word-limit
            v-model="hzsForm.password"
          ></el-input>
        </el-form-item>
      </el-form>
    </span>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeFn">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addHzs, editHzs } from '@/api/dianweiguanli/hezuoshang'
export default {
  props: {
    hzsVisible: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      hzsForm: {
        name: '',
        password: '',
        ratio: '',
        account: '',
        contact: '',
        mobile: ''
      },
      hzsRules: {
        name: [
          { required: true, message: '请输入合作商名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        contact: [
          { required: true, message: '请输入联系人名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入联系人电话', trigger: 'blur' },
          { pattern: /^1\d{10}$/, message: '手机号格式不正确' }
        ],
        ratio: [
          { required: true, message: '请输入分成比例', trigger: 'blur' }
          // { min: 0, max: 100, message: '分成比例在0-100之间', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 2, max: 18, message: '长度在 2 到 18 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {},
  mounted () {},
  methods: {
    // 弹窗关闭
    closeFn () {
      this.$emit('update:hzsVisible', false)
      this.$refs.hzsForm.resetFields()
      this.$emit('gengxin')
      this.hzsForm = {
        name: '',
        password: '',
        ratio: 0,
        account: '',
        contact: '',
        mobile: ''
      }
    },
    // 新增功能
    async submit () {
      try {
        await this.$refs.hzsForm.validate()
        if (this.hzsForm.id) {
          await editHzs(this.hzsForm)
        } else {
          await addHzs(this.hzsForm)
          this.$message.success('添加成功')
        }
        this.$emit('gengxin')
        this.closeFn()
      } catch (err) {
        console.log(err)
        this.$message.error(err.response.data)
      }
    }
  }
}
</script>
<style lang="less" scoped>
.el-button--default{
  background-color: #fbf4f0;
  border-color: #fbf4f0;
  color: #606266;
}
.el-button--primary{
  color: #fff;
  border-color: #ff9743;
  background-color: #ff9743;
}
.el-input-number__decrease{
  position:absolute;
  right: 0;
  top: 0;
}
</style>
